package com.mingqijia.gassafety.webserver.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.util.StringUtil;
import com.mingqijia.gassafety.authority.api.resp.config.DictionaryItemRespDTO;
import com.mingqijia.gassafety.authority.dal.helper.ConfigureHolder;
import com.mingqijia.gassafety.db.entity.FileUpload;
import com.mingqijia.gassafety.db.mapper.FileUploadMapper;
import com.mingqijia.gassafety.shared.constant.Constants;
import com.mingqijia.gassafety.shared.utils.ApplicationUtils;
import com.mingqijia.gassafety.shared.utils.commonUtils.BeanConvertUtils;
import com.mingqijia.gassafety.shared.utils.commonUtils.PageUtil;
import com.mingqijia.gassafety.webserver.request.FileUploadQueryRequest;
import com.mingqijia.gassafety.webserver.response.FileUploadResponse;
import com.mingqijia.gassafety.webserver.service.FileUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 文件上传记录 服务实现类
 * </p>
 *
 * @author wanghai
 * @since 2022-02-24
 */
@Service
public class FileUploadServiceImpl extends ServiceImpl<FileUploadMapper, FileUpload> implements FileUploadService {


    @Autowired
    ConfigureHolder holder;

    @Override
    public PageUtil<FileUploadResponse> getUploadList(FileUploadQueryRequest request) {
        String spId = ApplicationUtils.getWorkingSpId();
        Page<Object> page = PageHelper.startPage( request.getPage(), request.getPageSize() );

        QueryWrapper<FileUpload> fileUploadQueryWrapper = new QueryWrapper<>();
        fileUploadQueryWrapper.lambda().eq( FileUpload::getUploadCode,request.getUploadCode() )
                .eq( StringUtil.isNotEmpty( request.getStatus() ), FileUpload::getUploadStatus,request.getStatus() )
                .like( StringUtil.isNotEmpty( request.getOperatorName() ), FileUpload::getUploadCreator,request.getOperatorName() )
                .between( request.getUploadStartTime()!=null && request.getUploadEndTime()!=null ,
                        FileUpload::getCreatedAt,request.getUploadStartTime(),request.getUploadEndTime() )
                .eq( FileUpload::getIsDeleted, Constants.IS_DELETED_FALSE )
                .eq( FileUpload::getSpId,spId )
                .orderByDesc( FileUpload::getUploadTime);
        List<FileUpload> fileUploadList = this.list( fileUploadQueryWrapper );

        List<FileUploadResponse> responseList = BeanConvertUtils.convertListTo( fileUploadList, FileUploadResponse::new );
        List<DictionaryItemRespDTO> importStatus = holder.getDictionaryByKey("Equipment_import_status");
        for (FileUploadResponse response : responseList) {
            try {
                JSONArray array = JSONObject.parseArray(response.getJsonInfo());
                array.sort((o1, o2) ->{
                    JSONObject r1=JSONObject.parseObject(o1+"");
                    JSONObject r2=JSONObject.parseObject(o2+"");
                    int r1Number = handleParseInt(r1.get("number"));
                    int r2Number = handleParseInt(r2.get("number"));
                    return r1Number-r2Number;
                });
                response.setJsonInfoList(array);
                response.setJsonInfo("");
            } catch (Exception e) {
                log.error("getUploadList-error:{}",e);
                response.setJsonInfoList(new ArrayList<>());
            }
            for (DictionaryItemRespDTO status : importStatus) {
                if (status.getValue().equals( response.getUploadStatus() ))  response.setUploadStatus( status.getName() );
            }
        }
        PageUtil<FileUploadResponse> pageUtil = new PageUtil<>();
        pageUtil.setList( responseList);
        pageUtil.setPage( request.getPage() );
        pageUtil.setPageSize(request.getPageSize());
        pageUtil.setTotal(page.getTotal());
        return pageUtil;
    }

    private Integer handleParseInt(Object numberObj){
        int number = 0;
        try{
            number = Integer.parseInt(null==numberObj?"0":numberObj.toString());
        }catch (Exception ignored){}
        return number;
    }

}
